package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketRegion;
import com.cskaoyan.bean.po.MarketRegionExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.region.vo.RegionVO;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.cskaoyan.mapper.RegionCountyMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.service.adminservice.RegionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


/**
 * @author:J
 * @datetime 2022/12/29 16:08
 * @description: TODO:行政区域service层
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    RegionCountyMapper regionCountyMapper;

    @Override
    public CommonData<MarketRegion> list() {

        BaseRespVo baseRespVo = new BaseRespVo<>();

        //开启分页
        //PageHelper.startPage(info.getPage(),info.getLimit());

        //根据id查询数据，id不会为空，不校验。

        MarketRegionExample example = new MarketRegionExample();
//        //排序
//        //example.setOrderByClause(info.getSort()+" "+info.getOrder());
//
//        //建查询标准(条件)
        MarketRegionExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);

        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(example);
        //省表，pid是行政级别，type是省编号，市表查询id，省表查询pid=0
        List<RegionVO> list = null;

        //市表
        List<RegionVO> listCity = null;
        //区表
        List<RegionVO> listCounty = null;

        for (MarketRegion marketRegion : marketRegions) {
            list = regionMapper.selectByPid(marketRegion.getId());

            for (RegionVO regionVO : list) {

                listCity = regionMapper.selectByPid(regionVO.getId());

                for (RegionVO vo : listCity) {
                    listCounty = regionMapper.selectByPid(vo.getId());
                    vo.setChildren(listCounty);
                    regionVO.setChildren(listCity);
                }
            }
            marketRegion.setChildren(list);
        }
            PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions);

            return CommonData.data(pageInfo);

        }

}
